package cn.com.wangluotongxin.merchants.controller;

import cn.com.wangluotongxin.common.constants.CommonConstant;
import cn.com.wangluotongxin.common.web.AjaxResult;
import cn.com.wangluotongxin.db.pojo.PageData;
import cn.com.wangluotongxin.merchants.dto.RoleDto;
import cn.com.wangluotongxin.merchants.form.RoleEditForm;
import cn.com.wangluotongxin.merchants.form.RoleForm;
import cn.com.wangluotongxin.merchants.service.IMerchantsRoleService;
import cn.com.wangluotongxin.merchants.vo.RoleVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商户管理-角色信息 前端控制器
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-05-17
 */
@Api(tags = "商户管理-角色信息 相关接口")
@RestController
@RequestMapping("/role")
public class MerchantsRoleController {

    @Autowired
    private IMerchantsRoleService roleService;

    @ApiOperation("分页")
    @GetMapping("/page")
    public AjaxResult<PageData<RoleVo>> getPage(RoleDto dto){
        IPage<RoleVo> page = roleService.getRolePage(dto);
        return AjaxResult.success(PageData.build(page));
    }

    @ApiOperation("列表")
    @GetMapping("/list")
    public AjaxResult<List<RoleVo>> getList() {
        return AjaxResult.success(roleService.getList());
    }

    @ApiOperation("详情")
    @GetMapping("/detail/{id}")
    public AjaxResult<RoleVo> detail(@PathVariable("id") Integer id) {
        return AjaxResult.success(roleService.roleDetail(id));
    }

    @ApiOperation("添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody RoleForm form) {
        Assert.notNull(form.getRoleName(), "缺少名称");

        roleService.addRole(form);
        return AjaxResult.success();
    }

    @ApiOperation("编辑")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody RoleEditForm form) {
        Assert.notNull(form.getId(), "缺少数据ID");
        Assert.notNull(form.getRoleName(), "缺少名称");

        roleService.editRole(form);
        return AjaxResult.success();
    }
}